package com.ylr.blog.framework.controller.web;

import com.ylr.base.common.annotation.Resubmit;
import com.ylr.base.common.dto.PageDto;
import com.ylr.blog.common.dto.blog.BlogDto;
import com.ylr.base.common.jsr303.group.OrderSearch;
import com.ylr.base.common.tool.Result;
import com.ylr.blog.common.vo.blog.BlogFormVo;
import com.ylr.blog.common.vo.blog.BlogVo;
import com.ylr.blog.framework.service.BlogService;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-05-18 21:39:06
 * className: BlogWebController
 * version: 1.0
 * description:
 */
@Validated
@RestController
@RequestMapping("/web/blog")
@AllArgsConstructor
public class BlogWebController {

    private static final Logger log = LoggerFactory.getLogger(BlogWebController.class);

    private final BlogService blogService;

    /**
     * 保存博客接口
     * @param blogVo 参数对象
     */
    @Resubmit(spEL = "'saveBlog-'.concat(#userPo.id)", message = "保存博客")
    @PostMapping("/save")
    public Result<Object> saveBlog(@RequestBody BlogVo blogVo) {
        log.info("开始保存博客操作。{}", blogVo);

        blogService.saveBlog(blogVo);

        log.info("保存博客操作完成");
        return Result.success();
    }

    /**
     * 保存博客并提交审核接口
     * @param blogVo 参数对象
     * @return 未完成的属性提示
     */
    @Resubmit(spEL = "'saveAndCommitBlog-'.concat(#userPo.id)", message = "保存博客")
    @PostMapping("/saveAndCommit")
    public Result<String> saveAndCommitBlog(@Validated @RequestBody BlogVo blogVo) {
        log.info("开始保存博客并提交审核操作。{}", blogVo);

        String unfinishedField = blogService.saveAndCommitBlog(blogVo);

        log.info("保存博客并提交审核操作完成");
        return Result.success(unfinishedField);
    }

    /**
     * 分页查询用户博客接口
     * @param blogFormVo 参数对象
     * @return 用户博客分页数据
     */
    @GetMapping("/page/list")
    public Result<PageDto<BlogDto>> pageBlogList(@Validated(OrderSearch.class) @RequestBody BlogFormVo blogFormVo) {
        log.info("开始分页查询用户博客操作。{}", blogFormVo);

        PageDto<BlogDto> pageDto = blogService.pageBlogList(blogFormVo);

        log.info("分页查询用户博客操作结束");
        return Result.success(pageDto);
    }

    /**
     * 信息
     */
    @GetMapping("/get")
    public Result<Object> getYbBlog(@RequestParam("id") Long id) {
//		BlogEntity ybBlog = blogService.getById(id);

        return Result.success();
    }

    /**
     * 博客提交审核接口
     * @param id   博客主键
     * @param code 博客编号
     * @return 未完成的属性提示
     */
    @Resubmit(spEL = "'commitBlog-'.concat(#userPo.id)", message = "博客提交审核")
    @PostMapping("/commit")
    public Result<String> commitBlog(@RequestParam("id") Long id, @RequestParam("code") String code) {
        log.info("开始博客提交审核操作。id={} code={}", id, code);

        String unfinishedField = blogService.commitBlog(id, code);

        log.info("博客提交审核操作结束");
        return Result.success(unfinishedField);
    }

    /**
     * 撤销待审核博客接口
     * @param id   博客主键
     * @param code 博客编号
     */
    @Resubmit(spEL = "'withdrawAuditBlog-'.concat(#userPo.id)", message = "撤销待审核博客")
    @PostMapping("/withdraw/audit")
    public Result<Object> withdrawAuditBlog(@RequestParam("id") Long id, @RequestParam("code") String code) {
        log.info("开始博客撤销审核操作。id={} code={}", id, code);

        blogService.withdrawAuditBlog(id, code);

        log.info("博客撤销审核操作结束");
        return Result.success();
    }

    /**
     * 发布博客接口
     * @param id   博客主键
     * @param code 博客编号
     */
    @Resubmit(spEL = "'publishBlog-'.concat(#userPo.id)", message = "发布博客")
    @GetMapping("/publish")
    public Result<Object> publishBlog(@RequestParam("id") Long id, @RequestParam("code") String code) {
        log.info("开始博客撤销审核操作。id={} code={}", id, code);

        blogService.publishBlog(id, code);

        log.info("博客撤销审核操作结束");
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public Result<Object> deleteYbBlog(@RequestBody Long[] ids) {
//		blogService.removeByIds(Arrays.asList(ids));

        return Result.success();
    }

}
